package com.nullpointer.xchat;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.nullpointer.xchat.object.UserInfo.GenderType;
import com.nullpointer.xchat.utils.DataManager;
import com.nullpointer.xchat.utils.Utils;

public class LoginActivity extends Activity {

	// Main layout
	private EditText etName;
	private Spinner snRoom;
	private Button btJoinchat;
	private TextView tvNotice;

	private TextView tvMale;
	private TextView tvFemale;
	private TextView tvOther;

	private Animation animationShow;
	private Animation animationHide;

	private GenderType gender = GenderType.FEMALE;
	private int focus = Color.parseColor("#ff8800");
	private int normal = Color.parseColor("#333333");

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		findViewIDs();
		setEvent();
		init();
	}

	private void init() {
		animationShow = AnimationUtils.loadAnimation(this,
				R.anim.snackbar_show_animation);
		animationHide = AnimationUtils.loadAnimation(this,
				R.anim.snackbar_hide_animation);
		// etName.setText(DataManager.getInstance(this).getUserName());
		if (etName.getText() != null
				&& etName.getText().toString().trim().length() >= 3) {
			hideNotice();
		} else {
			showNotice();
		}
	}

	private void findViewIDs() {
		etName = (EditText) findViewById(R.id.et_name);
		snRoom = (Spinner) findViewById(R.id.sn_room);
		btJoinchat = (Button) findViewById(R.id.bt_joinchat);
		tvNotice = (TextView) findViewById(R.id.tv_notice);
		tvMale = (TextView) findViewById(R.id.tv_male);
		tvFemale = (TextView) findViewById(R.id.tv_female);
		tvOther = (TextView) findViewById(R.id.tv_other);
	}

	private void setEvent() {
		tvFemale.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tvFemale.setTextColor(focus);
				tvMale.setTextColor(normal);
				tvOther.setTextColor(normal);
				gender = GenderType.FEMALE;
			}
		});
		tvMale.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tvFemale.setTextColor(normal);
				tvMale.setTextColor(focus);
				tvOther.setTextColor(normal);
				gender = GenderType.MALE;
			}
		});
		tvOther.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tvFemale.setTextColor(normal);
				tvMale.setTextColor(normal);
				tvOther.setTextColor(focus);
				gender = GenderType.ANY;
			}
		});
		btJoinchat.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Utils.hideKeyboard(LoginActivity.this, etName);
				String name = etName.getText().toString();
				if (name == null || name.length() < 3) {
					return;
				}
				DataManager.getInstance(LoginActivity.this).setUserName(name);
				DataManager.getInstance(LoginActivity.this).setIsLogout(false);
				DataManager.getInstance(LoginActivity.this).setUserGender(
						gender);
				String female = "";
				if (gender == GenderType.FEMALE) {
					female = ". I'm female";
				}
				String status = "Hi! I'm " + name + female + ". Wanna chat with me?";
				DataManager.getInstance(LoginActivity.this).setUserStatus(
						status);
				// DataManager.getInstance(LoginActivity.this).setCurrentRoom(
				// RoomType.values()[snRoom.getSelectedItemPosition()]);
				finishAndCallBack();
			}
		});

		etName.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (etName.getText() != null
						&& etName.getText().toString().trim().length() >= 3) {
					hideNotice();
				} else {
					showNotice();
				}
			}
		});
	}

	private void showNotice() {
		if (tvNotice.getVisibility() == View.VISIBLE) {
			return;
		}
		btJoinchat.setVisibility(View.GONE);
		tvNotice.setVisibility(View.VISIBLE);
		tvNotice.startAnimation(animationShow);
	}

	private void hideNotice() {
		if (tvNotice.getVisibility() == View.GONE) {
			return;
		}
		animationHide.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				btJoinchat.setVisibility(View.VISIBLE);
				tvNotice.setVisibility(View.GONE);

			}
		});
		tvNotice.startAnimation(animationHide);
	}

	public void finishAndCallBack() {
		Intent returnIntent = new Intent();
		setResult(RESULT_OK, returnIntent);
		finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
